package com.jdjw.modules.busgeneralapply.service.impl;

import com.jdjw.common.ResponseResult;
import com.jdjw.modules.busassetapply.entity.Dept;
import com.jdjw.modules.busgeneralapply.entity.BusGeneralApply;
import com.jdjw.modules.busgeneralapply.entity.BusGeneralApplyVo;
import com.jdjw.modules.busgeneralapply.mapper.BusGeneralApplyMapper;
import com.jdjw.modules.busgeneralapply.service.BusGeneralApplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdjw.modules.sysdept.entity.SysDept;
import com.jdjw.modules.sysdept.mapper.SysDeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 一般性补助救助 服务实现类
 * </p>
 *
 * @author fq
 * @since 2021-07-21
 */
@Service
public class BusGeneralApplyServiceImpl extends ServiceImpl<BusGeneralApplyMapper, BusGeneralApply> implements BusGeneralApplyService {

    @Autowired
    private BusGeneralApplyMapper busGeneralApplyMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Override
    public ResponseResult queryAll(BusGeneralApplyVo busGeneralApplyVo) {
        ResponseResult result=new ResponseResult();
        try {
            //拿到数据起始位置
            int start=busGeneralApplyVo.getPage()*busGeneralApplyVo.getRow()-busGeneralApplyVo.getRow();
            busGeneralApplyVo.setPage(start);
            List<BusGeneralApply> busGeneralApplies = busGeneralApplyMapper.queryAll(busGeneralApplyVo);

            for (BusGeneralApply busGeneralApply : busGeneralApplies) {
                SysDept sysDept = sysDeptMapper.selectById(busGeneralApply.getOrgCode());
                if(sysDept!=null){
                    busGeneralApply.setOrgCodeName(sysDept.getName());
                }
                busGeneralApply.setUserName(busGeneralApplyVo.getUserName());
            }

            if ("CJTCSQ".equals(busGeneralApplyVo.getNodeLogo())) {
                for (BusGeneralApply apply : busGeneralApplies) {
                    if ("3".equals(apply.getStatus())) {
                        apply.setStatus("6");
                    }
                }
            }
            result.setMsg("查询成功");
            result.setCode(200);
            result.setData(busGeneralApplies);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("查询失败");
            result.setCode(500);
            result.setData(null);
        }

        return result;
    }

    @Override
    public ResponseResult sqlEdit(String sqlEdit) {
        ResponseResult rrt=new ResponseResult();
        Boolean aBoolean = busGeneralApplyMapper.sqlEdit(sqlEdit);
        if(aBoolean){
            rrt.setCode(200);
            rrt.setMsg("编辑成功");
        }else{
            rrt.setCode(500);
            rrt.setMsg("编辑失败");
        }
        return rrt;
    }
}
